<?php
//活动信息服务

namespace App\Services;

use App\Repositories\ActivitiesRepository;
use Carbon\Carbon;

class ActivityService
{


    /**
     * 获取列表
     * @param $query
     * @param $offset
     * @param $limit
     * @param $orderType
     * @return array|mixed
     */
    public static function getList($query = [], $offset = 0, $limit = 20, $orderType = 0)
    {
        //获取证书关联信息
        $repo = new ActivitiesRepository();
        $repo->buildQuery($query);
        $list = $repo
            ->getList($offset, $limit, $orderType)
            ->toarray();

        return self::getListFormat($list);
    }

    /**
     * 获取单条
     * @param $query
     * @return array
     */
    public static function getRow($query = [])
    {
        //获取证书关联信息
        $repo = new ActivitiesRepository();
        $repo->buildQuery($query);
        $row = $repo
            ->getRow();
        if (!$row) {
            return [];
        }
        $row = $row->toArray();
        $list = self::getListFormat([$row]);

        return reset($list);
    }

    /**
     * 获取总数
     * @param $query
     * @return array
     */
    public static function getTotal($query = [])
    {
        //获取证书关联信息
        $repo = new ActivitiesRepository();
        $repo->buildQuery($query);

        return $repo->getTotal();
    }

    /**
     * 获取页面结构列表
     * @param $query
     * @param $page
     * @param $pageSize
     * @return array
     */
    public static function getPageList($query = [], $page = 1, $pageSize = 20, $orderType = 0)
    {
        $output = [
            'total'     => 0,
            'totalPage' => 0,
            'pageSize'  => (int) $pageSize,
            'list'      => [],
        ];
        //获取证书关联信息
        $repo = new ActivitiesRepository();
        $repo->buildQuery($query);
        $total = $repo->getTotal();
        if (!$total) {
            return $output;
        }
        $offset = ($page - 1) * $pageSize;
        $output['total'] = $total;
        $output['totalPage'] = (int) ceil($total / $pageSize);
        $list = $repo->getList($offset, $pageSize, $orderType)->toarray();
        $output['list'] = self::getListFormat($list);


        return $output;
    }


    //活动状态
    public static $activityStatusList = [
        1 => '未开始',
        2 => '进行中',
        3 => '已结束',
    ];

    //上线状态
    public static $statusList = [
        1 => '草稿',
        2 => '上线',
    ]; //活动状态

    /**
     * 格式化列表信息
     * @param $list
     * @return array|mixed
     */
    public static function getListFormat($list = [])
    {
        if (!$list) {
            return [];
        }
        $nowDate = Carbon::now()->toDateTimeString();
        foreach ($list as &$val) {
            //活动状态
            $activityStatus = 0;
            if ($val['receive_date_start'] > $nowDate) {
                $activityStatus = 1;
            } elseif ($val['receive_date_end'] > $nowDate) {
                $activityStatus = 2;
            } else {
                $activityStatus = 3;
            }
            $val['activity_status'] = $activityStatus;
            $val['activity_status_text'] = self::$activityStatusList[$activityStatus] ?? '';
            //上线状态
            $val['status_text'] = self::$statusList[$val['status']] ?? '';

        }
        unset($val);

        return $list;
    }

    /**
     * 保存
     * @param $query
     * @return array
     */
    public static function save($data = [])
    {
        if (empty($data['title'])) {
            return resultError('参数缺失');
        }
        //处理券批次id格式
        if (!empty($data['stock_ids']) && is_array($data['stock_ids'])) {
            $data['stock_ids'] = implode(',', $data['stock_ids']);
        }
        $repo = new ActivitiesRepository();
        if(!empty($data['id'])) {
            $res = $repo->updateFields($data['id'], $data);
        } else {
            $res = $repo->save($data);
        }
        if (!$res) {
            return resultError('保存失败');
        }

        return resultSuccess([
            'id' => $res,
        ], '保存成功');
    }

    /**
     * 逻辑删除
     * @param $id
     * @return bool
     */
    public static function del($id)
    {
        if (!$id) {
            return false;
        }
        $res = (new ActivitiesRepository())->updateFields($id, [
            'deleted_at' => Carbon::now()->toDateTimeString(),
        ]);
        if (!$res) {
            return false;
        }

        return true;
    }

}
